package com.company.pdd2021;

import java.util.*;

public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        char[][] map = new char[n][m];
        int t_x = 0,t_y = 0;
        int num = 0;
        for (int i = 0; i < n; i++) {
            String line = sc.next();
            for (int j = 0; j < m; j++) {
                if(line.charAt(j) == 'T'){
                    t_x = i;
                    t_y = j;
                }else if(line.charAt(j) == 'X'){
                    num++;
                }
                map[i][j] = line.charAt(j);
            }
        }
        // 广度优先搜索
        int[][] dp = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                dp[i][j] = Integer.MAX_VALUE;
            }
        }
        boolean[][] isUsed = new boolean[n][m];
        dp[t_x][t_y] = 0;
        isUsed[t_x][t_y] = true;
        int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{t_x, t_y});
        int start = 0;
        String[][] data = new String[num][2];
        while (!queue.isEmpty()){
            int[] point = queue.poll();
            int x = point[0];
            int y = point[1];
            for (int[] d:directions){
                int new_x = x + d[0];
                int new_y = y + d[1];
                if(new_x < 0 || new_x >= n || new_y < 0|| new_y >= m || isUsed[new_x][new_y]){
                    continue;
                }else if(map[new_x][new_y] == '1'){
                    dp[new_x][new_y] = Integer.MAX_VALUE;
                }else if(map[new_x][new_y] == '0'){
                    dp[new_x][new_y] = Math.min(dp[new_x][new_y],dp[x][y] + 1);
                    queue.offer(new int[]{new_x,new_y});

                }else if(map[new_x][new_y] == 'X'){
                    dp[new_x][new_y] = Math.min(dp[new_x][new_y],dp[x][y] + 1);
                    data[start][0] = new_x + " " + new_y;
                    data[start][1] = String.valueOf(dp[new_x][new_y]);
                    start++;

//                    System.out.println(new_x + " " +  new_y);
//                    System.out.println(dp[new_x][new_y]);
//                    System.out.println("---");

                }
                isUsed[new_x][new_y] = true;
            }
        }

//        for (int i = 0; i < num ; i++) {
//            System.out.println(data[i][0]);
//            System.out.println(data[i][1]);
//            System.out.println("---");
//        }
        Arrays.sort(data,(a,b) ->{
            if(Integer.valueOf(a[1]) != Integer.valueOf(b[1])){
                return Integer.parseInt(a[1]) - Integer.parseInt(b[1]);
            }else{
                String[]line1 = a[0].split(" ");
                String[]line2 = b[0].split(" ");
                if(Integer.valueOf(line1[0]) != Integer.valueOf(line2[0])){
                    return Integer.parseInt(line1[0]) - Integer.parseInt(line2[0]);
                }else{
                    return Integer.parseInt(line1[1]) - Integer.parseInt(line2[1]);
                }
            }
        });
//        for (int i = 0; i < num ; i++) {
//            System.out.println(data[i][0]);
//            System.out.println(data[i][1]);
//            System.out.println("---");
//        }
        int min = Integer.parseInt(data[0][1]);
        List<String> res = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            if(min == Integer.parseInt(data[i][1])){
                String[] line1 = data[i][0].split(" ");
                res.add(line1[0]);
                res.add(line1[1]);
            }else{
                break;
            }
        }
        System.out.println(min);
        for (int i = 0; i < res.size(); i++) {
            if(i == 0){
                System.out.print(res.get(i));
            }else{
                System.out.print(" ");
                System.out.print(res.get(i));
            }
        }

    }
}
